package com.clannad.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clannad.eduservice.entity.EduTeacher;
import com.clannad.eduservice.mapper.EduTeacherMapper;
import com.clannad.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-07-31
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    @Cacheable(value = "teacher",key = "'teacher'")
    public List<EduTeacher> getTeacherIndex() {
        LambdaQueryWrapper<EduTeacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduTeacher::getSort,1);
        lambdaQueryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        lambdaQueryWrapper.last("limit 4");
        List<EduTeacher> list = this.list(lambdaQueryWrapper);
        return list;
    }

    @Override
    @CacheEvict(value = "teacher",allEntries = true)
    public boolean updateTeacher(EduTeacher teacher) {
        removeRedis();
        return this.updateById(teacher);
    }



    @Override
    @CacheEvict(value = "teacher",allEntries = true)
    public boolean saveTeacher(EduTeacher teacher) {
        removeRedis();
        return  this.save(teacher);
    }

    @Override
    @CacheEvict(value = "teacher",allEntries = true)
    public boolean removeTeacher(String id) {
        removeRedis();
        return this.removeById(id);
    }

    @Override
    public Map<Object, Object> getTeacherFrontList(Page<EduTeacher> page1) {
        LambdaQueryWrapper<EduTeacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(EduTeacher::getSort);
        this.page(page1,lambdaQueryWrapper);
        List<EduTeacher> records = page1.getRecords();
        long current = page1.getCurrent();
        long pages = page1.getPages();
        long size = page1.getSize();
        long total = page1.getTotal();
        boolean hasNext = page1.hasNext();
        boolean hasPrevious = page1.hasPrevious();
        Map<Object, Object> map=new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    private void removeRedis() {
        Set<String> keys = redisTemplate.keys("teacherFront"  + "*");
        redisTemplate.delete(keys);
    }
}
